package com.render.game;

import com.badlogic.gdx.math.Vector2;

public class PhysicsCamera {

	public Vector2 rotationOrigin = new Vector2();
	public float rotation = 0;
	
	public boolean updated = false;
	
	public Vector2 scaleWorldToScreen = new Vector2();
	public Vector2 scaleScreenToWorld = new Vector2();
	
	
	public Vector2 screenMin;
	public Vector2 screenMax;
	
	public Vector2 worldMin;
	public Vector2 worldMax;
	
	public Vector2 screenMinInWorld;
	public Vector2 screenMaxInWorld;
	
	
	public PhysicsCamera(Vector2 worldMin, Vector2 worldMax, Vector2 screenMin, Vector2 screenMax,
			Vector2 screenMinInWorld, Vector2 screenMaxInWorld){
		
		setCamera(worldMin, worldMax, screenMin, screenMax,screenMinInWorld, screenMaxInWorld);
		
	}
	
	public PhysicsCamera(Vector2 worldMax, Vector2 screenMax, Vector2 screenMinInWorld, Vector2 screenMaxInWorld){
		setCamera(new Vector2(), worldMax, new Vector2(), screenMax, screenMinInWorld, screenMaxInWorld );
	}
	
	public PhysicsCamera(Vector2 worldMax, Vector2 screenMax){
		setCamera(new Vector2(), worldMax, new Vector2(), screenMax, new Vector2(), worldMax);
	}
	
	public void setCamera(Vector2 worldMin, Vector2 worldMax, Vector2 screenMin, Vector2 screenMax,
			Vector2 screenMinInWorld, Vector2 screenMaxInWorld){
		
		scaleWorldToScreen.x = (screenMax.x - screenMin.x)/(screenMaxInWorld.x - screenMinInWorld.x);
		scaleWorldToScreen.y = (screenMax.y - screenMin.y)/(screenMaxInWorld.y - screenMinInWorld.y);
		
		scaleScreenToWorld.x = 1/scaleWorldToScreen.x;
		scaleScreenToWorld.y = 1/scaleWorldToScreen.y;
		
		this.screenMin = screenMin;
		this.screenMax = screenMax;
		this.worldMin = worldMin;
		this.worldMax = worldMax;
		this.screenMinInWorld = screenMinInWorld;
		this.screenMaxInWorld = screenMaxInWorld;
	}
	
	public Vector2 worldToScreen(float x, float y){
		return worldToScreen(new Vector2(x,x));
	}
	
	public Vector2 worldToScreen(Vector2 world){
		Vector2 screen = new Vector2();
		screen.x = screenMin.x + scaleWorldToScreen.x * (world.x - worldMin.x - screenMinInWorld.x);
		screen.y = screenMin.y + scaleWorldToScreen.y * (world.y - worldMin.y - screenMinInWorld.y);
		
		return screen;
	}
	
	public Vector2 screenToWorld(float x, float y){
		return screenToWorld(new Vector2(x,y));
	}
	
	public Vector2 screenToWorld(Vector2 screen){
		Vector2 world = new Vector2();
		world.x = (screen.x - screenMin.x)*scaleScreenToWorld.x + worldMin.x + screenMinInWorld.x;
		world.y = (screen.y - screenMin.y)*scaleScreenToWorld.y + worldMin.y + screenMinInWorld.y;
		return world;
	}
	
	
	
	public float worldWidth(){
		return (worldMax.x - worldMin.x);
	}
	
	public float worldHeight(){
		return (worldMax.y - worldMin.y);
	}
	
	public float screenWidth(){
		return (screenMax.x - screenMin.x);
	}
	
	public float screenHeight(){
		return (screenMax.y - screenMin.y);
	}
	
	public String toString(){
		String s = "";
		s += screenMin.toString()+" -> "+screenToWorld(screenMin).toString()+"\n";
		s += screenMax.toString()+" -> "+screenToWorld(screenMax).toString();
		return s;
	}
	
	public void reshape(float x1, float y1, float x2, float y2){
		this.screenMin.set(x1, y1);
		this.screenMax.set(x2, y2);
		
		setCamera(worldMin, worldMax, screenMin, screenMax, screenMinInWorld, screenMaxInWorld);
		
	}
	
	public void pan(Vector2 pan){
		screenMinInWorld = screenMinInWorld.add(pan);
		screenMaxInWorld = screenMaxInWorld.add(pan);
		
		updated = true;
	}
	
	public void zoom(Vector2 zoom){
		screenMinInWorld = screenMinInWorld.add(zoom);
		screenMaxInWorld = screenMaxInWorld.sub(zoom);
		
		updated = true;
	}
	
	public void rotate(float amount){
		rotation += amount;
		
		updated = true;
	}
}
